#include "DATray.h"
#include "DAFuncTools.h"

#include <QApplication>

DATray::DATray(QWidget *parent)
    : QSystemTrayIcon(parent)
{
//    p = parent;
    daSetting = new DASetting();
    setIcon(DAFunTools::StdIcon(12));
    setToolTip(tr("桌面助手"));
    connect(this, &DATray::activated, this, &DATray::onIconActivated);

    trayMenu = new QMenu();		// 新建菜单，指定父对象
    // 新建菜单子项
    QAction *action1 = new QAction(trayMenu);
    QAction *action2 = new QAction(trayMenu);

    action1->setIcon(DAFunTools::StdIcon(33));	// 设置菜单图标
    action1->setText(QString("设置"));		// 设置带单文本
    action2->setIcon(DAFunTools::StdIcon(40));
    action2->setText(QString("退出"));


    // 添加菜单子项
    trayMenu->addAction(action1);
    trayMenu->addAction(action2);

    // 显示窗体
    connect(action1, &QAction::triggered, [=](bool) {
        daSetting->show();
    });
    // 退出
    connect(action2, &QAction::triggered, [=](bool) { exit(0); });
}

void DATray::onIconActivated(QSystemTrayIcon::ActivationReason reason) {
    if (reason == QSystemTrayIcon::Trigger)
    {// 点击触发

    }
    else if (reason == QSystemTrayIcon::Context)
    {// 右键触发
        trayMenu->exec(QCursor::pos());	// 在鼠标的位置显示
    }
}

bool DATray::event(QEvent * event)
{
    if (QEvent::ActivationChange == event->type()) {
        if (trayMenu->isVisible()){
            trayMenu->close();
        }
    }
    return QSystemTrayIcon::event(event);
}
